
#include	"SysConfig.h"
#include	"Define.h"
#include	"Function.h"
#include	"Power.h"
#include	"Audio.h"
#ifdef  LC786XXX
#include "LC786XXX.h"
#endif
#include	"Aux_Function.h"
#include	"Display.h"
#include	"UI_Main.h"
#ifdef  BLUETOOTH
#include	"Bluetooth.h"
#ifdef  MOVON
#include	"Movon.h"
#endif
#endif
#include	"Port.h"
#ifdef  CD
#include	"CD.h"
#include	"CD_MECHA.h"
#endif
#include    "Tuner.h"
#include    "USB.h"
#ifdef  HERO
#include	"Hero_Drv.h"
#endif
#ifdef  ATOMIC
#include	"Atomic_DRV.h"
#endif
#ifdef  MIC
#include	"MIC.h"
#endif
#ifdef  AV_CONTROLLER
#include	"AVController.h"
#endif
#ifdef  AUTO_GUIDE
#include	"AutoGuide.h"
#endif
#include	"Model_Option.h"

UI_FUNCTION ucFUNC_TargetFunction;
unsigned char ucFUNC_FunctionSeq;
unsigned char ucFUNC_Timer10msec;
unsigned char ucFUNC_AbnormalRevovery;

UI_FUNCTION ucFUNC_History[FUNC_NUMBER];
UI_FUNCTION ucFUNC_MediaHistory[FUNC_NUMBER];
UI_FUNCTION ucFUNC_Current;
UI_FUNCTION ucFUNC_CurrentMedia;
unsigned char ucFUNC_Power;

//////////////////////////////////////////////////////////////////////////////////////////////
void FUNC_SavePreviousFunction(UI_FUNCTION  ucFunc);
unsigned char FUNC_CheckFunctionAvailable(UI_FUNCTION ucFunc);
UI_FUNCTION FUNC_GetPreviousFunction(void);
unsigned char FUNC_FunctionPlay(UI_FUNCTION ucFunc);
unsigned char FUNC_WaitFunctionPlay(UI_FUNCTION ucFunc);

//////////////////////////////////////////////////////////////////////////////////////////////

#ifdef  LC786XXX
extern unsigned char ucLC786XXX_USB_Detected;
extern unsigned short usLC786XXX_USB_TotalFile;
extern unsigned short usLC786XXX_InitUSB_10mecTimer;
extern LC786X_MEDIA_ID  ucLC786XXX_CurrentMediaID;
extern unsigned char	ucLC786XXX_USB_Process;

extern void LC786XXX_MediaStop(UI_FUNCTION ucFunc);
extern unsigned char LC786XXX_USB_GetPlayStatus(void);
extern unsigned char ucLC786XXX_InitializeComplete;
#ifdef  LC786XXX_AUDIO
extern unsigned char ucLC786XXX_InitAudio;
#endif

#ifdef  LC78696X
extern void LC786XXX_GoToSleep(void);
extern unsigned char	ucLC786XXX_CD_Process;
#endif

#endif

extern unsigned char ucPWR_A2DP_Resume;
extern void PWR_SetAntennaActive(unsigned char ucMode);

extern USB_STATUS ucUSB_Status;

#ifdef  CD
extern void CD_SetTextStatus(unsigned char ucDes);

extern CD_STATUS ucCD_Status;
extern unsigned char ucDECK_Disc;
extern unsigned char ucDECK_Mode;

#endif

#ifdef  BLUETOOTH
#ifdef  MOVON
extern MV_HF_AUDIO ucMV_HF_AudioConnect;	// 0 : Phone, 1 : Audio
extern MV_HF_STATUS ucMV_HFS_Status;
extern MV_AV_STATUS ucMV_A2DP_Status;
extern MV_AVRCP_STATUS ucMV_AVRCP_Status;
extern MV_PB_STATUS ucMV_PBAP_Status;
extern unsigned char ucMV_InitOk;
extern unsigned char BT_PairingCancel(void);
#endif
extern void BT_SetProcess(unsigned char ucMode);
extern void BT_AVRCP_Command(BT_AVRCP ucCmd);
extern void BT_CallHistoryDownLoad_Request(void);
extern void BT_SetVolume(void);
extern void BT_BluetoothVolumeInit(void);
extern BT_PROC ucBT_Process;
extern unsigned char ucBT_AutoConnection;
#endif

#ifdef  HERO
extern unsigned char ucHERO_BootSeq;
#endif
#ifdef  ATOMIC
extern unsigned char ucATOMIC_BootSeq;
#endif
extern void TUNER_SetBand(void);
extern void TUNER_BandChangeStart(void);
extern void TUNER_PowerOff(void);
extern unsigned char TUNER_BandChange_TotalBand(unsigned char ucDes);
extern void TUNER_BandInit(void);
extern void TUNER_SetDCDC(unsigned char ucBand, unsigned short usFreq);
extern unsigned char ucTUNER_PowerOn;

extern unsigned char ucPWR_AccStatus;
extern void AUDIO_SetMute(unsigned char ucDes);
extern void AUDIO_VolumeInit(void);
extern void	AUDIO_SetVolume(void);

extern void MENU_SetID(UI_MENU_ID ucID);
extern unsigned char ucAUDIO_BeepOut;

extern unsigned char PWR_ProcessIsIdle(void);

unsigned char FUNC_GetPowerFlag(void)
{
	return(ucFUNC_Power);
}
/*////////////////////////////////////////////////////////////////////////////
Title : unsigned char FUNC_PowerStatus(void)
return :
	0x00 : in Power Off
	0x01 : in Power On
////////////////////////////////////////////////////////////////////////////*/
unsigned char FUNC_PowerStatus(void)
{
	unsigned char ucPower = 0;
	
	if  (ucFUNC_Power == 1)
	{
		ucPower = 1;
	}
	if  (ucFUNC_FunctionSeq != 0)
	{
		if  ((ucFUNC_FunctionSeq & FUNC_POWER_ON_SEQ0) == FUNC_POWER_ON_SEQ0)
		{
			ucPower = 1;
		}
		if  ((ucFUNC_FunctionSeq & FUNC_POWER_OFF_SEQ0) == FUNC_POWER_OFF_SEQ0)
		{
			ucPower = 0;
		}
	}
//	if  (ucPWR_AccStatus == 0)
//	{
//		ucPower = 0;
//	}
	return(ucPower);
}

UI_FUNCTION	FUNC_GetCurrentFunction(void)
{
	if  (ucFUNC_FunctionSeq == 0)
	{
		return(ucFUNC_Current);
	}
	return(ucFUNC_TargetFunction);
}

void FUNC_SavePreviousFunction(UI_FUNCTION  ucFunc)
{
	unsigned char i;
	unsigned char ucBuf[FUNC_NUMBER];

#if	0//def  BLUETOOTH
	if  (ucFunc == FUNC_HF)
	{
		return;
	}
#endif

#ifdef  BLUETOOTH
	if  (ucFunc == FUNC_LPB)
	{
		return;
	}
#endif

//	if  ((ucFunc == ucFUNC_History[0]) || (FUNC_CheckFunctionAvailable(ucFunc) == 0))
	if  (ucFunc == ucFUNC_History[0])
	{
		return;
	}
	
	memcpy(&ucBuf[0], &ucFUNC_History[0], FUNC_NUMBER);
	memcpy(&ucFUNC_History[1], &ucBuf[0], FUNC_NUMBER - 1);
	ucFUNC_History[0] = ucFunc;
}
void FUNC_SavePreviousMedia(UI_FUNCTION  ucFunc)
{
	unsigned char i;
	unsigned char ucBuf[FUNC_NUMBER];

	LOG_FUNC("FUNC_SavePreviousMedia:%d",ucFunc);

#if	0//def  BLUETOOTH
	if  (ucFunc == FUNC_HF)
	{
		return;
	}
#endif
	if  (ucFunc == FUNC_TUNER)
	{
		return;
	}
	
#ifdef  BLUETOOTH
	if	(ucFunc == FUNC_LPB)
	{
		return;
	}
#endif  
#ifdef  AV_CONTROLLER
	if  (ucFunc == FUNC_AVC_MODE)
	{
		return;
	}
#endif
	ucFUNC_CurrentMedia = ucFunc;
	
//	if  ((ucFunc == ucFUNC_MediaHistory[0]) || (FUNC_CheckFunctionAvailable(ucFunc) == 0))
	if  (ucFunc == ucFUNC_MediaHistory[0])
	{
		return;
	}

	memcpy(&ucBuf[0], &ucFUNC_MediaHistory[0], FUNC_NUMBER);
	memcpy(&ucFUNC_MediaHistory[1], &ucBuf[0], FUNC_NUMBER - 1);
	ucFUNC_MediaHistory[0] = ucFunc;

}

unsigned char FUNC_FunctionChangeStatus(void)
{
	if  (ucFUNC_FunctionSeq == FUNC_SEQ_NONE)
	{
		return(0);
	}
	return(1);
}

unsigned char FUNC_FunctionChangeStart(UI_FUNCTION ucTarget, unsigned char ucMode)
{	
	LOG_BT("FUNC_FunctionChangeStart");

	DISP_SetCurrentDisplay();
	#ifdef  AUTO_GUIDE
	if	(AG_GetAnnounceStatus() == AG_ANNOUNCE_OFF)
	#endif
	{
		AUDIO_SetMute(3);
	}
#ifdef  BLUETOOTH
	if  (FUNC_GetCurrentFunction() == FUNC_LPB)
	{
		MV_TX_SYS_StopSpkMicLoopBack();
	}
#endif
	ucFUNC_TargetFunction = ucTarget;

	ucFUNC_FunctionSeq = ucMode;

	if  ((ucMode & FUNC_CHANGE_SEQ0) == FUNC_CHANGE_SEQ0)
	{
		if  (ucFUNC_Power == 0)
		{
			ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ0;
		}
	}
	DISP_PageScrollReset();

	FUNC_SavePreviousFunction(ucTarget);
	FUNC_SavePreviousMedia(ucTarget);

	LOG_FUNC("FUNC_FunctionChangeStart:T%d,S%X",ucTarget,ucMode);

	return(1);
}

void FUNC_SaveCurrentFunction(UI_FUNCTION ucFunc)
{
	FUNC_SavePreviousFunction(ucFunc);
	FUNC_SavePreviousMedia(ucFunc);
}

void FUNC_AbnormalRecovery(void)
{
	AUDIO_MuteOn();
	ucFUNC_AbnormalRevovery = 1;
	FUNC_FunctionChangeStart(FUNC_GetCurrentFunction(),FUNC_POWER_ON_SEQ0);
}

/*////////////////////////////////////////////////////////////////////////
Title : unsigned char FUNC_CheckFunctionAvailable(UI_FUNCTION ucFunc)
ucRet :
	0 : Function None
	1 : Function OK
////////////////////////////////////////////////////////////////////////*/
unsigned char FUNC_CheckFunctionAvailable(UI_FUNCTION ucFunc)
{
	unsigned char ucRet = 0;
	LC786X_ERROR_CODE	ucErrorCode; 
	
	LOG_FUNC("FUNC_CheckFunctionAvailable:%d",ucFunc);
	switch  (ucFunc)
	{
		case  FUNC_TUNER:
			ucRet = 1;
			break;
		case FUNC_AUX:
			if  (ucAUX_Detected == 1)
			{
				ucRet = 1;
			}
			break;
		case FUNC_USB:
			if  (ucLC786XXX_USB_Detected == 1)
			{
				ucRet = 1;
				if  (ucUSB_Status == USB_ST_ERROR)
				{
					ucRet = 0;
				}
				if  (ucUSB_Status == USB_ST_MOUNTED)
				{
					if  (usLC786XXX_USB_TotalFile == 0)
					{
						ucRet = 0;
					}
					else
					{
						ucRet = 1;
					}
				}
			}
			break;
	#ifdef  CD
		case FUNC_CD:
			if  ((ucCD_Status == CD_STATUS_NONE) || (ucCD_Status == CD_STATUS_NOFILE) || (ucCD_Status == CD_STATUS_ERROR))
			{
				ucRet = 0;
			}
			else
			{
				ucRet = 1;
			}
			if  (ucCD_Status == CD_STATUS_ERROR)
			{
				ucRet = 0;
			}
			if  (DECK_CurrentMode_Eject() == 1)
			{
				ucRet = 0;
			}
			break;
	#endif
	#ifdef  BLUETOOTH
		case FUNC_A2DP:
			if  (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
			{
				ucRet = 1;
			}
			break;
		case FUNC_HF:
			if  (ucMV_HFS_Status > MV_HFS_CONNECTED)
			{
				ucRet = 1;
			}
			break;
		case FUNC_LPB:
			ucRet = 1;
			break;
	#endif
	#ifdef  AV_CONTROLLER
		case FUNC_AVC_MODE:
			if  (AVC_GetMode() != AVC_MODE_OFF)
			{
				ucRet = 1;
			}
			break;
	#endif
	}

	return(ucRet);
}

UI_FUNCTION FUNC_GetPreviousFunction(void)
{
	unsigned char temp = FUNC_TUNER;
	unsigned char i,j = 0;

	LOG_FUNC("FUNC_GetPreviousFunction");

	for(i = 1; i < FUNC_NUMBER; ++i)
	{
	#ifdef  BLUETOOTH
		if  ((FUNC_CheckFunctionAvailable(ucFUNC_History[i]) == 1) && (ucFUNC_History[i]) != FUNC_HF)
	#else
		if  (FUNC_CheckFunctionAvailable(ucFUNC_History[i]) == 1)
	#endif
		{
			temp = ucFUNC_History[i];
			break;
		}
	}
	
	for(j = 0; i < FUNC_NUMBER; ++i)
	{
		ucFUNC_History[j] = ucFUNC_History[i];
		++j;
	}
	for(; j < FUNC_NUMBER; ++j)
	{
		ucFUNC_History[j] = FUNC_TUNER;
	}

#ifdef  BLUETOOTH
	if  (temp == FUNC_LPB)
	{
		temp = FUNC_TUNER;
	}
#endif
	if  (temp == FUNC_AVC_MODE)
	{
		AVC_SetKeyCommand(AVC_KEY_RETURN_MODE);
	}
	return(temp);
}

UI_FUNCTION FUNC_GetPreviousMedia(void)
{
	unsigned char temp = 0x00;
	unsigned char i,j = 0;
	unsigned char ucFunc = 0;

	for(i = 1; i < FUNC_NUMBER; ++i)
	{
		if  (ucFUNC_MediaHistory[i] != FUNC_TUNER)
		{
	#ifdef  BLUETOOTH
			if  ((FUNC_CheckFunctionAvailable(ucFUNC_MediaHistory[i]) == 1) && (ucFUNC_MediaHistory[i]) != FUNC_HF)
	#else
			if  (FUNC_CheckFunctionAvailable(ucFUNC_MediaHistory[i]) == 1)
	#endif
			{
				temp = ucFUNC_MediaHistory[i];
				break;
			}
		}
	}

	for(j = 0; i < FUNC_NUMBER; ++i)
	{
		ucFUNC_MediaHistory[j] = ucFUNC_MediaHistory[i];
		++j;
	}

#ifdef  BLUETOOTH
	if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
	{
		ucFunc = FUNC_A2DP;
	}
#endif
	if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
	{
		ucFunc = FUNC_AUX;
	}
	if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 1)
	{
		ucFunc = FUNC_USB;
	}
#ifdef  CD
	if  (FUNC_CheckFunctionAvailable(FUNC_CD) == 1)
	{
		ucFunc = FUNC_CD;
	}
#endif
	for(; j < FUNC_NUMBER; ++j)
	{
		ucFUNC_MediaHistory[j] = ucFunc;
	}

	if  (temp == 0)
	{
		return(ucFunc);
	}
	
#ifdef  BLUETOOTH
	if  (temp == FUNC_LPB)
	{
		temp = FUNC_TUNER;
	}
#endif
	return(temp);
}

unsigned char FUNC_WaitStop(void)
{
	unsigned char ucRet = 0;

#ifdef  CD
	if  (ucFUNC_TargetFunction== FUNC_CD)
	{
//		if  (LC786XXX_GetMediaStatus(FUNC_USB) < LC786X_MS_TOC_READ)
		{
			ucRet = 1;
		}
	}
#endif

	if  (ucFUNC_TargetFunction == FUNC_USB)
	{
	#ifdef  CD
//		if  (LC786XXX_GetMediaStatus(FUNC_CD) < LC786X_MS_TOC_READ)
//		{
			ucRet = 1;
//		}
//		else
//		{
//			LOG_FUNC("FUNC_WaitStop:%d",LC786XXX_GetMediaStatus(FUNC_CD));
//		}
		if  ((DRV_LC786XXX_GetCDProcess() & 0xF0) == LC786XXX_PROCESS_EJECT)
		{
			ucRet = 0;
		}
	#else
		ucRet = 1;
	#endif
	}

	if  (ucFUNC_TargetFunction == FUNC_TUNER)
	{
		ucRet = 1;
	}
	if  (ucFUNC_TargetFunction == FUNC_AUX)
	{
		ucRet = 1;
	}
#ifdef  BLUETOOTH
	if  (ucFUNC_TargetFunction == FUNC_A2DP)
	{
		ucRet = 1;
	}
	if  (ucFUNC_TargetFunction == FUNC_HF)
	{
		ucRet = 1;
	}
	if  (ucFUNC_TargetFunction == FUNC_LPB)
	{
		ucRet = 1;
	}
#endif
#ifdef  AV_CONTROLLER
	if  (ucFUNC_TargetFunction == FUNC_AVC_MODE)
	{
		ucRet = 1;
	}
#endif
	return(ucRet);
}

void FUNC_FunctionStop(void)
{
	unsigned char ucStatus;
	
#ifdef  BLUETOOTH
//////////////// Bluetooth Power Off /////////////////////
	if	((ucFUNC_Current == FUNC_A2DP) && (ucMV_A2DP_Status == MV_A2DP_STREAMING))
	{
		BT_AVRCP_Command(BT_AVRCP_PLAY_PAUSE);
		ucFUNC_Timer10msec = 1000/10;
	}
#endif

	if  (ucFUNC_Current == FUNC_TUNER)
	{
		TUNER_ClearTunerMode();
	}
	
	if  (AVC_GetPS_Status() == AVC_PS_ON)
	{
		return;
	}
#if  0
	if  (ucLC786XXX_InitializeComplete == 1)
	{
		if  (ucFUNC_Current == FUNC_USB)
		{
			if  ((ucLC786XXX_USB_Process == LC786XXX_PROCESS_CHECK_PLAY) || (LC786XXX_USB_GetPlayStatus() == 1))
			{
				LC786XXX_MediaStop(FUNC_USB);
			}
		}
	#ifdef CD
		if  (ucFUNC_Current == FUNC_CD)
		{
		//////////////// CD Power Off /////////////////////
			ucStatus = LC786XXX_GetMediaStatus(FUNC_CD);

			if  ((ucLC786XXX_CD_Process == LC786XXX_PROCESS_CHECK_PLAY) || (ucStatus > LC786X_MS_STOP))
			{
				LC786XXX_MediaStop(FUNC_CD);
			}
		}
	#endif
	}
#endif
	USB_ScanOff();
	USB_RandomOff();
	USB_PauseOff(0);
	USB_ClearFastPlayFlag();
#ifdef  CD
	CD_PauseOff(0);
	CD_ScanOff();
	CD_RandomOff();
	CD_ClearFastPlayFlag();
#endif
}

/*////////////////////////////////////////////////////////////////////////////
Title : unsigned char FUNC_FunctionPlay(UI_FUNCTION ucFunc)
Return 
	-. 0x00 : Wait
	-. 0x01 : OK
	-. 0x02 : Cancel Function
/////////////////////////////////////////////////////////////////////////////*/

unsigned char FUNC_FunctionPlay(UI_FUNCTION ucFunc)
{
	unsigned char ucDes = 0;
	
	switch  (ucFunc)
	{
		case  FUNC_TUNER:
		#ifdef  AV_CONTROLLER
			if  (OPT_GetOption(OPT_MASK_AVC) == 0)
		#endif
			{
				TUNER_BandChangeStart();
			}
			ucDes = 1;
			break;
		case FUNC_AUX:
			ucDes = 1;
			break;
		case FUNC_USB:
			if  (LC786XXX_GetMediaStatus(FUNC_USB) == LC786X_MS_DEVICE_DISCONNECT)
			{
				if  (ucLC786XXX_USB_Detected == 0)
				{
					ucDes = 2;	
				}
			}
			else
			{
				LC786XXX_ChangeToUSB();
				DISP_PageScrollReset();
				ucDes = 1;
			}
			break;
	#ifdef  CD
		case FUNC_CD:
			if  (ucDECK_Disc == 1)
			{
				LC786XXX_ChangeToCD();
				DISP_SetCDPlayIcon();
				DISP_PageScrollReset();
				ucDes = 1;				
			}
			else
			{
				if  (ucDECK_Mode == DECK_MODE_NONE)
				{
					ucDes = 2;
				}
			}
			break;
	#endif
	#ifdef  BLUETOOTH
		case FUNC_A2DP:
			{
				if  (ucBT_Process == BT_PRS_AVRCP_CMD)
				{
					ucDes = 0;
				}
				else
				{
					switch  (ucMV_A2DP_Status)
					{
						case MV_A2DP_CONNECTABLE:
							if  (ucPWR_A2DP_Resume == 1)
							{
								ucDes = 0;
							}
							else
							{
								ucDes = 2;
							}
							LOG_FUNC("MV_A2DP_CONNECTABLE");
							break;
						case MV_A2DP_SIGNALING_CHANNEL_CONNECTED:
							ucDes = 1;
							LOG_FUNC("MV_A2DP_SIGNALING_CHANNEL_CONNECTED");
							break;
						case MV_A2DP_CONNECTED:
							BT_AVRCP_Command(BT_AVRCP_PLAY_PAUSE);
							ucDes = 1;
							LOG_FUNC("MV_A2DP_CONNECTED");
							break;
						case MV_A2DP_STREAMING:
							ucDes = 1;
							LOG_FUNC("MV_A2DP_STREAMING");
							break;
						case MV_A2DP_PAUSED:
							BT_AVRCP_Command(BT_AVRCP_PLAY_PAUSE);
							ucDes = 1;
							LOG_FUNC("MV_A2DP_PAUSED");
							break;
					}
				}
			}
			break;
		case FUNC_HF:
			ucDes = 1;
			break;
		case FUNC_LPB:
			ucDes = 1;
			break;
	#endif
	#ifdef  AV_CONTROLLER
		case FUNC_AVC_MODE:
			if  (AVC_GetMode() != AVC_MODE_OFF)
			{
				ucDes = 1;
			}
			break;
	#endif
	}

	LOG_FUNC("FUNC_FunctionPlay:%d:%d", ucFunc, ucDes);
	return(ucDes);
}

unsigned char FUNC_WaitFunctionPlay(UI_FUNCTION ucFunc)
{
	unsigned char ucDes = 0;

	switch  (ucFunc)
	{
		case  FUNC_TUNER:
			if  (ucTUNER_PowerOn == 1)
			{
				ucDes = 1;
			}
			break;
		case FUNC_AUX:
			ucDes = 1;
			break;
		case FUNC_USB:
			if  (LC786XXX_GetMediaStatus(FUNC_USB) == LC786X_MS_PLAYBACK)
			{
				ucDes = 1;
			}
			break;
	#ifdef  CD
		case FUNC_CD:
			if  ((LC786XXX_GetMediaStatus(FUNC_CD) == LC786X_MS_PLAYBACK)
				|| (LC786XXX_GetErrorCode(ucLC786XXX_CurrentMediaID) != LC786X_EC_NoError))
			{
				ucDes = 1;
			}
			break;
	#endif
	#ifdef  BLUETOOTH
		case FUNC_A2DP:
			{
				switch  (ucMV_A2DP_Status)
				{
					case MV_A2DP_CONNECTABLE:
						ucDes = 0;
						break;
					case MV_A2DP_SIGNALING_CHANNEL_CONNECTED:
						ucDes = 0;
						break;
					case MV_A2DP_CONNECTED:
						ucDes = 0;
						break;
					case MV_A2DP_STREAMING:
						ucDes = 1;
						break;
					case MV_A2DP_PAUSED:
						ucDes = 0;
						break;
				}
			}
			break;
		case FUNC_LPB:
		case FUNC_HF:
			ucDes = 1;
			break;
	#endif
	#ifdef  AV_CONTROLLER
		case FUNC_AVC_MODE:
			ucDes = 1;
			break;
	#endif
	}
	return(ucDes);
}


void FUNC_PowerOn(void)
{
	DISP_ClearTempDisplayMode();
	MENU_Clear();	
	if  (FUNC_CheckFunctionAvailable(ucFUNC_Current) == 1)
	{
		FUNC_FunctionChangeStart(ucFUNC_Current,FUNC_POWER_ON_SEQ0);
	}
	else
	{
		FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(),FUNC_POWER_ON_SEQ0);
	}
	
}

void FUNC_PowerOff(void)
{
	if  (ucFUNC_Power == 1)
	{
	#ifdef BLUETOOTH	
		BT_PairingCancel();	
		MENU_SetID(MENU_NONE);
	#endif	
		FUNC_FunctionChangeStart(FUNC_GetCurrentFunction(),FUNC_POWER_OFF_SEQ0);

		if  (OPT_GetOption(OPT_MASK_CLOCK) == 1)
		{			
			PWR_DisplayDriverOff();
			PWR_DisplayDriverOn();
		}
		else
		{
			PWR_PowerControl(PWR_BACKLIGHT, 0);
		}
	}
}

unsigned char FUNC_SelectUSB(void)
{
	if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 0)
	{
		return(0);
	}

	FUNC_FunctionChangeStart(FUNC_USB,FUNC_CHANGE_SEQ0);
	
	return(1);
}
#ifdef  CD
unsigned char FUNC_SelectCD(void)
{
	if  (FUNC_CheckFunctionAvailable(FUNC_CD) == 0)
	{
		return(0);
	}

	FUNC_FunctionChangeStart(FUNC_CD,FUNC_CHANGE_SEQ0);
	
	return(1);
}
#endif

unsigned char FUNC_SelectAux(void)
{
	if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 0)
	{
		return(0);
	}

	FUNC_FunctionChangeStart(FUNC_AUX,FUNC_CHANGE_SEQ0);
	
	return(1);
}

unsigned char FUNC_TotalFunctionChange(void)
{
	unsigned char ucDes = 0;
	unsigned char ucRet = 1;
	unsigned char ucFunc;

	if  (FUNC_CheckFunctionAvailable(FUNC_AUX))
	{
		++ucDes;
	}
	#ifdef  BLUETOOTH
	if  (FUNC_CheckFunctionAvailable(FUNC_A2DP))
	{
		++ucDes;
	}
	#endif
	if  (FUNC_CheckFunctionAvailable(FUNC_USB))
	{
		++ucDes;
	}	
	#ifdef  CD
	if  (FUNC_CheckFunctionAvailable(FUNC_CD))
	{
		++ucDes;
	}
	#endif

	switch  (FUNC_GetCurrentFunction())
	{
		case FUNC_TUNER:
			if  (TUNER_BandChange_TotalBand(0) == 0)
			{
				if  (ucDes == 0)
				{
					TUNER_BandChange_TotalBand(1);
				}
				else
				{
					#ifdef  CD
					if  (FUNC_CheckFunctionAvailable(FUNC_CD) == 1)
					{
						ucFunc = FUNC_CD;
					}
					else
					#endif
					if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 1)
					{
						ucFunc = FUNC_USB;
			
					}
					else  if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
					{
						ucFunc = FUNC_AUX;
					}
					#ifdef  BLUETOOTH
					else  if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
					{
						ucFunc = FUNC_A2DP;
					}
					#endif
				}
			}
			else
			{
				return(1);
			}
			break;
	#ifdef  CD
		case FUNC_CD:
			if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 1)
			{
				ucFunc = FUNC_USB;
	
			}
			else  if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
			{
				ucFunc = FUNC_AUX;
			}
			#ifdef  BLUETOOTH
			else if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
			{
				ucFunc = FUNC_A2DP;
			}
			#endif
			else
			{
				ucFunc = FUNC_TUNER;
				TUNER_BandInit();
			}
			break;
	#endif
		case FUNC_USB:
			if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
			{
				ucFunc = FUNC_AUX;
			}
			#ifdef  BLUETOOTH
			else if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
			{
				ucFunc = FUNC_A2DP;
			}
			#endif
			else
			{
				ucFunc = FUNC_TUNER;
				TUNER_BandInit();
			}
			break;
		case FUNC_AUX:
			#ifdef  BLUETOOTH
			if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
			{
				ucFunc = FUNC_A2DP;
			}
			else
			#endif
			{
				ucFunc = FUNC_TUNER;
				TUNER_BandInit();
			}
			break;
	#ifdef  BLUETOOTH
		case FUNC_A2DP:
			ucFunc = FUNC_TUNER;
			TUNER_BandInit();
			break;
	#endif
	}
	
	FUNC_FunctionChangeStart(ucFunc, FUNC_CHANGE_SEQ0);

	return(ucRet);
}

unsigned char FUNC_MediaChange(void)
{
	unsigned char ucDes = 0;
	unsigned char ucRet = 1;
	unsigned char ucFunc;

	if  (ucFUNC_CurrentMedia == FUNC_TUNER)
	{
	#ifdef  CD
		if  (OPT_GetOption(OPT_MASK_CD))
		{
			ucFUNC_CurrentMedia = FUNC_CD;
		}
		else
		{
			ucFUNC_CurrentMedia = FUNC_USB;
		}
	#else
		ucFUNC_CurrentMedia = FUNC_USB;
	#endif
	}

	if  (FUNC_CheckFunctionAvailable(FUNC_AUX))
	{
		++ucDes;
	}
	#ifdef  BLUETOOTH
	if  (FUNC_CheckFunctionAvailable(FUNC_A2DP))
	{
		++ucDes;
	}
	#endif
	if  (FUNC_CheckFunctionAvailable(FUNC_USB))
	{
		++ucDes;
	}	
	#ifdef  CD
	if  (OPT_GetOption(OPT_MASK_CD))
	{
		if  (FUNC_CheckFunctionAvailable(FUNC_CD))
		{
			++ucDes;
		}
	}
	#endif
	
	if  ((FUNC_GetCurrentFunction() == FUNC_TUNER) || (FUNC_GetCurrentFunction() == FUNC_AVC_MODE))
	{
		if  (ucDes == 0)
		{
			ucRet = 0;//DISP_SetTempDisplayMode(TDISP_NO_MECH);// No Media Display
			DISP_SetTempDisplayMode(DISP_NO_MECH);
		}
		else
		{
			if  ((ucDes == 1) && (FUNC_CheckFunctionAvailable(FUNC_AUX)))
			{
				ucFUNC_CurrentMedia = FUNC_AUX;
			}
			ucFunc = ucFUNC_CurrentMedia;
			if  (FUNC_CheckFunctionAvailable(ucFunc) == 1)
			{
				FUNC_FunctionChangeStart(ucFunc,FUNC_CHANGE_SEQ0);
			}
			else
			{
				FUNC_FunctionChangeStart(FUNC_GetPreviousMedia(), FUNC_CHANGE_SEQ0);
			}
		}
	}
	else
	{
		if  (ucDes > 1)
		{
			#ifdef  CD
			if  (FUNC_GetCurrentFunction() == FUNC_CD)
			{
				if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 1)
				{
					ucFunc = FUNC_USB;
		
				}
				else  if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
				{
					ucFunc = FUNC_AUX;
				}
				#ifdef  BLUETOOTH
				else  if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
				{
					ucFunc = FUNC_A2DP;
				}
				#endif
			}
			#endif
			if  (FUNC_GetCurrentFunction() == FUNC_USB)
			{
				if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
				{
					ucFunc = FUNC_AUX;
				}
				#ifdef  BLUETOOTH
				else  if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
				{
					ucFunc = FUNC_A2DP;
				}
				#endif
				#ifdef  CD
				else  if  (FUNC_CheckFunctionAvailable(FUNC_CD) == 1)
				{
					ucFunc = FUNC_CD;
				}
				#endif
			}
			if  (FUNC_GetCurrentFunction() == FUNC_AUX)
			{
				#ifdef  BLUETOOTH
				if  (FUNC_CheckFunctionAvailable(FUNC_A2DP) == 1)
				{
					ucFunc = FUNC_A2DP;
				}
				else
				#endif
				#ifdef  CD
				if  (FUNC_CheckFunctionAvailable(FUNC_CD) == 1)
				{
					ucFunc = FUNC_CD;
				}
				else
				#endif
				if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 1)
				{
					ucFunc = FUNC_USB;
				}
			}
			#ifdef  BLUETOOTH
			if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
			{
				#ifdef  CD
				if  (FUNC_CheckFunctionAvailable(FUNC_CD) == 1)
				{
					ucFunc = FUNC_CD;
				}
				else
				#endif
				if  (FUNC_CheckFunctionAvailable(FUNC_USB) == 1)
				{
					ucFunc = FUNC_USB;
				}
				else  if  (FUNC_CheckFunctionAvailable(FUNC_AUX) == 1)
				{
					ucFunc = FUNC_AUX;
				}
			}
			#endif
			FUNC_FunctionChangeStart(ucFunc, FUNC_CHANGE_SEQ0);
		}
		else
		{
			ucRet = 0;// No media display?
		}
	}	

	return(ucRet);
}

void FUNC_AudioVolume(UI_FUNCTION ucFunc)
{
	#ifdef  AUTO_GUIDE
	if	(AG_GetAnnounceStatus() == AG_ANNOUNCE_ON)
	{
		return;
	}
	#endif
	switch  (ucFunc)
	{
		case FUNC_TUNER:
			AUDIO_SetVolume();
			break;
	#ifdef  BLUETOOTH
		case FUNC_HF:
		case FUNC_A2DP:
		case FUNC_LPB:
			BT_BluetoothVolumeInit();
			BT_SetVolume();
			break;
	#endif
		default:
			AUDIO_SetVolume();
			break;
	}
}

void FUNC_SetInput(UI_FUNCTION ucFunc)
{	
	if  ((MIC_GetMicStatus() == MIC_ON) && (MIC_GetMixMode() == MIX_OFF))
	{
		HERO_PhoneOnOff(1);
		HERO_PhoneMix(0);
	}
	else
	{
		AUDIO_InputSelect(ucFunc);
	}
}

void FUNC_Main(void)
{
	if  (ucFUNC_Timer10msec != 0)
	{
		return;
	}

	if  (ucAUDIO_BeepOut == 1)
	{
		return;
	}
	
	if  (PWR_ProcessIsIdle() == 0)
	{
		return;
	}
	
	switch  (ucFUNC_FunctionSeq)
	{
		case FUNC_POWER_ON_SEQ0:
			LOG_FUNC("FUNC_POWER_ON_SEQ0");
			ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ1;
			ucFUNC_Timer10msec = 100/10;
			if  (ucFUNC_AbnormalRevovery == 0)
			{
				AUDIO_VolumeInit();
			}
			PWR_PowerControl(PWR_CTR2, 1);
			if  (OPT_GetOption(OPT_MASK_CLOCK) == 0)
			{
				PWR_DisplayDriverOn();
			}
			PWR_SetAntennaActive(0x01);
		#ifdef  AV_CONTROLLER
			AVC_ProcessStart();
		#endif
//			TUNER_SetPowerOn();
//			LC786XXX_InitalizeStart();
			// Requestt Init Tuner/Amp/CD/USB/Bluetooth
			break;
		case (FUNC_POWER_ON_SEQ1):
		#ifdef  HERO
			if  (ucHERO_BootSeq == HERO_BOOT_SEQ_FINISH)
			{
			}
			else
			{
				ucFUNC_Timer10msec = 50/10;
				return;
			}
		#endif
		
		#ifdef  ATOMIC
			if  (ucATOMIC_BootSeq == ATOMIC_SEQ_FINISH)
			{
			}
			else
			{
				ucFUNC_Timer10msec = 50/10;
				return;
			}
		#endif
		
		#ifdef  LC786XXX_AUDIO
			if  (ucLC786XXX_InitAudio == 0)
			{
				return;
			}
		#endif
		
		#ifdef  AUTO_GUIDE
			AG_ProcessStart();
		#endif
		
		#ifdef  LC786XXX
			if  (ucFUNC_TargetFunction == FUNC_USB)
			{
				if  ((ucLC786XXX_InitializeComplete == 1) && (usLC786XXX_InitUSB_10mecTimer == 0))
				{
					ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ2;
				}
				else
				{
					ucFUNC_Timer10msec = 50/10;
				}
			}
			#ifdef  CD
			else  if  (ucFUNC_TargetFunction == FUNC_CD)
			{
				if  (ucLC786XXX_InitializeComplete == 1)
				{
					ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ2;
				}
				else
				{
					ucFUNC_Timer10msec = 50/10;
				}
			}
			#endif
			else
			{
				ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ2;
			}
		#if  0
			#ifdef  CD
			if  ((ucFUNC_TargetFunction == FUNC_USB) || (ucFUNC_TargetFunction == FUNC_CD))
			#else
			if  (ucFUNC_TargetFunction == FUNC_USB)
			#endif
			{
				if  (ucLC786XXX_InitializeComplete == 1)
				{
					ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ2;
				}
				else
				{
					ucFUNC_Timer10msec = 50/10;
				}
			}
			else
			{
				ucFUNC_FunctionSeq = FUNC_POWER_ON_SEQ2;
			}
		#endif
		#endif
			break;
		case FUNC_POWER_ON_SEQ2:
			LOG_FUNC("FUNC_POWER_ON_SEQ2");
			AUDIO_SetAudio();
		#ifdef  AV_CONTROLLER
			if  (OPT_GetOption(OPT_MASK_AVC) == 1)
			{
				TUNER_BandChangeStart();
				if  (AVC_GetPS_Status() == AVC_PS_ON)
				{
					AVC_PS_SourcePlay();
				}
			}
		#endif			
			ucFUNC_Power = 1;
			ucFUNC_AbnormalRevovery = 0;
			ucFUNC_FunctionSeq = FUNC_CHANGE_SEQ2;
			break;
		case FUNC_CHANGE_SEQ0:	// For Mute On
			LOG_FUNC("FUNC_CHANGE_SEQ0");
			ucFUNC_FunctionSeq = FUNC_CHANGE_SEQ1;
			ucFUNC_Timer10msec = 100/10;
			
	#ifdef  AUTO_GUIDE
			if	(AG_GetAnnounceStatus() == AG_ANNOUNCE_OFF)
	#endif
			{
				AUDIO_MuteOn();
				AUDIO_SetMute(6);
				
				if  (OPT_GetOption(OPT_MASK_AVC))
				{
					if  (ucFUNC_TargetFunction != FUNC_AVC_MODE)
					{
						AVC_SetKeyCommand(AVC_KEY_MUTE_ON);
					}
				}
			}
			break;
		case FUNC_CHANGE_SEQ1:
			LOG_FUNC("FUNC_CHANGE_SEQ1");
		#ifdef  LC786XXX
		#ifdef  LC78696X
			if  ((ucLC786XXX_USB_Process == LC786XXX_PROCESS_MEDIA_CHANGE) || ((ucLC786XXX_CD_Process == LC786XXX_PROCESS_MEDIA_CHANGE)))
		#else
			if  (ucLC786XXX_USB_Process == LC786XXX_PROCESS_MEDIA_CHANGE)
		#endif
		#endif
			{
				return;
			}	
		#ifdef  CD			
			if  ((DRV_LC786XXX_GetCDProcess() & 0xF0) == LC786XXX_PROCESS_EJECT)
			{
				return;
			}
		#endif
			FUNC_FunctionStop();
			ucFUNC_FunctionSeq = FUNC_CHANGE_SEQ2;
			ucFUNC_Timer10msec = 500/10;
			if  (ucFUNC_Current == FUNC_AVC_MODE)
			{
				AVC_SetKeyCommand(AVC_KEY_DVDP_OFF);
			}
			break;
		case FUNC_CHANGE_SEQ2:
			if  (FUNC_WaitStop() == 1)
			{
				unsigned char ucDes = FUNC_FunctionPlay(ucFUNC_TargetFunction);
				
				if  (ucDes == 1)
				{
					FUNC_SetInput(ucFUNC_TargetFunction);
					if  (ucFUNC_TargetFunction != FUNC_TUNER)
					{
						TUNER_SetDCDC(FM, 0);
					}
					ucFUNC_FunctionSeq = FUNC_CHANGE_SEQ3;
					LOG_FUNC("FUNC_CHANGE_SEQ2");
					FUNC_AudioVolume(ucFUNC_TargetFunction);
					if  (OPT_GetOption(OPT_MASK_AVC))
					{
						PWR_PowerControl(PWR_ANT, 1);
					}
					else
					{
						if  (ucFUNC_TargetFunction == FUNC_TUNER)
						{
							PWR_PowerControl(PWR_ANT, 1);
						}
						else
						{
							PWR_PowerControl(PWR_ANT, 0);
						}				
					}
				}
				if  (ucDes == 2)
				{
					ucFUNC_TargetFunction = FUNC_GetPreviousFunction();
					ucFUNC_FunctionSeq = FUNC_CHANGE_SEQ1;
				}
			}
			ucFUNC_Timer10msec = 50/10;
			break;
		case (FUNC_CHANGE_SEQ3):
			{
				unsigned char ucDes;

				ucDes = FUNC_WaitFunctionPlay(ucFUNC_TargetFunction);
				if  (ucDes == 1)
				{
					ucFUNC_Timer10msec = 100/10;
					ucFUNC_FunctionSeq = FUNC_CHANGE_SEQ4;
					ucFUNC_Current = ucFUNC_TargetFunction;
					AUDIO_SetMute(4);
					LOG_FUNC("FUNC_CHANGE_SEQ3");

				#ifdef  LC78696X
					if  (ucFUNC_Current == FUNC_USB)
					{
					}
				#ifdef  CD
					else  if  (ucFUNC_Current == FUNC_CD)
					{
					}
				#endif
					else
					{						
						if  (AVC_GetPS_Status() == AVC_PS_OFF)
						{
							LC786XXX_GoToSleep();
						}
					}
				#endif
				}
			}
			break;
		case (FUNC_CHANGE_SEQ4):		// Release Mute
			LOG_FUNC("FUNC_CHANGE_SEQ4");
			ucFUNC_Timer10msec = 0;
			ucFUNC_FunctionSeq = 0;
			AUDIO_MuteOff();
			AUDIO_SetMute(5);
		#ifdef  AV_CONTROLLER
	//		if  (OPT_GetOption(OPT_MASK_AVC))
	//		{
	//			if  (FUNC_GetCurrentFunction() != FUNC_AVC_MODE)
	//			{
	//				AVC_SetKeyCommand(AVC_KEY_DVDP_OFF);
	//			}
	//		}
	//		AVC_SetKeyCommand(AVC_KEY_MUTE_OFF);
		#endif
			break;
	#if  1
		case FUNC_POWER_OFF_SEQ0:
			ucFUNC_FunctionSeq = FUNC_POWER_OFF_SEQ1;
			ucFUNC_Current = ucFUNC_TargetFunction;
			PWR_PowerControl(PWR_ANT, 0);
		#ifdef  AV_CONTROLLER
			if  (OPT_GetOption(OPT_MASK_AVC))
			{
				AVC_SetKeyCommand(AVC_KEY_MUTE_ON);
				ucFUNC_Timer10msec = 200/10;
			}
			else
			{
				ucFUNC_Timer10msec = 0;
			}
		#endif
			if  (OPT_GetOption(OPT_MASK_CLOCK) == 0)
			{
				PWR_PowerControl(PWR_BACKLIGHT, 0);
				PWR_DisplayDriverOff();
			}
			break;
		case FUNC_POWER_OFF_SEQ1:
			AUDIO_SetMute(6);
			AUDIO_MuteOn();
			TUNER_PowerOff();
			PWR_PowerControl(PWR_CTR2, 0);
			PWR_SetAntennaActive(0x00);
			ucFUNC_Timer10msec = 100/10;
			ucFUNC_FunctionSeq = FUNC_POWER_OFF_SEQ2;
			break;
		case FUNC_POWER_OFF_SEQ2:
		#ifdef  AUTO_GUIDE
			AG_ProcessStop();
		#endif
		#ifdef  AV_CONTROLLER
			AVC_ProcessStop();
		#endif
			FUNC_FunctionStop();
			LC786XXX_GoToSleep();
			ucFUNC_FunctionSeq = FUNC_POWER_OFF_SEQ3;	
			break;
		case FUNC_POWER_OFF_SEQ3:
			if  (FUNC_WaitStop() == 1)
			{
				ucFUNC_Power = 0;
				ucFUNC_FunctionSeq = 0;
			}
			break;
	#else
		case FUNC_POWER_OFF_SEQ0:
		#ifdef  AV_CONTROLLER
			AVC_SetKeyCommand(AVC_KEY_MUTE_ON);
		#endif
			AUDIO_MuteOn();
			TUNER_PowerOff();
			PWR_PowerControl(PWR_ANT, 0);
			PWR_SetAntennaActive(0x00);
			ucFUNC_Current = ucFUNC_TargetFunction;
			ucFUNC_FunctionSeq = FUNC_POWER_OFF_SEQ1;
			ucFUNC_Timer10msec = 50/10;
			break;
		case FUNC_POWER_OFF_SEQ1:
			LOG_FUNC("FUNC_POWER_OFF_SEQ1");
			ucFUNC_FunctionSeq = FUNC_POWER_OFF_SEQ2;
			FUNC_FunctionStop();
			break;
		case (FUNC_POWER_OFF_SEQ2):
			LOG_FUNC("FUNC_POWER_OFF_SEQ2");
			if  (FUNC_WaitStop() == 1)
			{
				ucFUNC_FunctionSeq = FUNC_POWER_OFF_SEQ3;	
			}
			break;
		case (FUNC_POWER_OFF_SEQ3):
			LOG_FUNC("FUNC_POWER_OFF_SEQ3");
			ucFUNC_Power = 0;
			ucFUNC_FunctionSeq = 0;
			break;
	#endif
	}
}



